package reflect;

import java.io.File;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.net.URISyntaxException;

/**
 * 调用与当前类Test7在同一个包中所有类里那些本类自己定义的公开，无参且名字中含有字母s的方法
 */
public class Test7 {
    public static void main(String[] args) throws URISyntaxException, ClassNotFoundException, InstantiationException, IllegalAccessException, InvocationTargetException {
        Class c = Test7.class;
        String packName = c.getPackage().getName();

        File dir = new File(c.getResource(".").toURI());
        File[] files = dir.listFiles(f->f.getName().endsWith(".class"));
        for(File f : files){
            String fileName = f.getName().replace(".class","");
            Class cls = Class.forName(packName+"."+fileName);
            Object o = cls.newInstance();

            Method[] methods = cls.getMethods();
            for(Method m : methods){
                if(m.getModifiers() == Modifier.PUBLIC && m.getParameterCount() == 0 && m.getName().contains("s")){
                    m.invoke(o);
                }
            }
        }
    }
}
